//
//  dumpFile.m
//  ScreenRecord_ios_oc
//
//  Created by 51talk on 2021/5/24.
//

#import "DumpFile.h"

@implementation DumpFile

NSString *documentsDir;
NSOutputStream *oStream;

- (void)writeToFile:(unsigned char*)buffer size:(int)size {
    [oStream write:buffer maxLength:size];
}

- (void)openFileStream:(int)width height:(int)height {
    NSLog(@"打开文件流");
    documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    //yuv文件
    documentsDir = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%d*%d.yuv",width,height]];
    NSLog(@"documentsDir:%@", documentsDir);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isExit = [fileManager fileExistsAtPath:documentsDir];
    if (!isExit) {
        NSLog(@"yuv文件不存在,创建文件");
        [fileManager createFileAtPath:documentsDir contents:nil attributes:nil];
    } else {
        NSLog(@"删除已存在的yuv文件");
        [fileManager removeItemAtPath:documentsDir error:NULL];
        [fileManager createFileAtPath:documentsDir contents:nil attributes:nil];
    }
    
    oStream = [[NSOutputStream alloc] initToFileAtPath:documentsDir append:YES];
    [oStream open];
}

- (void)closeFileStream {
    NSLog(@"关闭文件流");
    [oStream close];
    oStream = nil;
}

@end
